﻿/*
 * This file is part of MonoSettlers.
 *
 * Copyright (C) 2010-2011 Christoph Husse
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: 
 *      # Christoph Husse
 * 
 * Also checkout our homepage: http://opensettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MonoSettlers.RenderSystem
{
    /// <summary>
    /// Is intended for oriented animation classes like settlers and soldiers.
    /// It will automatically use the position trackers direction value to
    /// select the correct animation.
    /// </summary>
    public class OrientedAnimationClass : RenderableAnimationClass
    {
        static private readonly Object m_ConstructorLock = new object();
        static private AnimationSet[] m_DirToAnim;

        private Direction m_Direction;

        /// <summary>
        /// The animation class is expected to provide one animation set for each angle, (Angle_000, Angle_045, etc.).
        /// </summary>
        public OrientedAnimationClass(GLRenderer inParent, AnimationClass inAnimClass) : base(inParent, inAnimClass)
        {
            lock (m_ConstructorLock)
            {
                if (m_DirToAnim == null)
                {
                    m_DirToAnim = new AnimationSet[8];

                    for (int i = 0; i < 8; i++)
                    {
                        var set = inAnimClass.FindSet("Angle" + ((Direction)i));

                        m_DirToAnim[i] = set;
                    }
                }
            }
        }

        /// <summary>
        /// Automatically select the correct animation set according to object's direction.
        /// </summary>
        internal override void Render()
        {
            if (m_Direction != PositionTracker.Direction)
            {
                m_Direction = PositionTracker.Direction;

                Play(m_DirToAnim[(int)m_Direction]);
            }

            base.Render();
        }
    }
}
